package com.revenant.driver.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.apkfuns.logutils.LogUtils;
import com.bumptech.glide.Glide;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.revenant.driver.R;
import com.revenant.driver.adapter.PersonalCommentAdapter;
import com.revenant.driver.base.BaseActivity;
import com.revenant.driver.bean.InviteCode;
import com.revenant.driver.bean.PersonalCommentShowBean;
import com.revenant.driver.bean.ShipperBean.PersonalDetailBean;
import com.revenant.driver.utils.GsonUtil;
import com.revenant.driver.utils.SPUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.revenant.driver.utils.Constants.Comment_PerSonal_Show;
import static com.revenant.driver.utils.Constants.Get_personal_info;
import static com.revenant.driver.utils.Constants.SelectInvite;
import static com.revenant.driver.utils.Constants.UpdateInvite;

public class PersonalActivity extends BaseActivity {
    @BindView(R.id.upload_phote)
    ImageView uploadPhote;

    String imageurl = "";
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.phone)
    TextView phone;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.smartrefresh)
    SmartRefreshLayout smartrefresh;

    PersonalCommentAdapter personalCommentAdapter;
    @BindView(R.id.jiaoyi)
    TextView jiaoyi;
    @BindView(R.id.jiehuo)
    TextView jiehuo;
    @BindView(R.id.pingjia)
    TextView pingjia;
    @BindView(R.id.invite_code)
    EditText inviteCodeEdit;

    private Context context;
    String inviteCodeText = "";
    private boolean isinput = true;

    @Override
    public int getContentViewResId() {
        context = this;
        return R.layout.activity_personal;
    }

    @Override
    public void initView() {
        setcenterTitle("个人信息");
//        Bundle bundle = getIntent().getExtras();
//        imageurl = bundle.getString("imageUrl");
//        Glide.with(this).load(imageurl).into(uploadPhote);

        inviteCodeEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 4 && isinput) {
                    updateInvite(s.toString());
                }

            }
        });
        getScore();
        getpseronal();
//        selectInvite();
        personalCommentAdapter = new PersonalCommentAdapter();
        recyclerview.setLayoutManager(new LinearLayoutManager(context));
        recyclerview.setAdapter(personalCommentAdapter);
        smartrefresh.autoRefresh();
        smartrefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getScore();
            }
        });

        selectInvite();

    }

    @Override
    public void leftbarclick() {
        super.leftbarclick();
        finish();
//        startActivity(MineDriverActivity.class);
    }

    @Override
    public void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.upload_phote)
    public void onViewClicked() {
        startActivity(UploadPhotoActivity.class);
    }

    private void getpseronal() {

        OkGo.<String>get(Get_personal_info)

                .params("id", SPUtils.getAccounId(context))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        LogUtils.d("AAAA" + response.body());
                        PersonalDetailBean g = GsonUtil.parseJsonWithGson(response.body(), PersonalDetailBean.class);
                        if (g.getCode().equals("0")) {

                            if (g.getData().getMobile() != null) {
                                StringBuilder stringBuilder = new StringBuilder(g.getData().getMobile());
                                stringBuilder.replace(3, 7, "***");
                                phone.setText(stringBuilder.toString());
                                name.setText(g.getData().getUsername());
                                Glide.with(context).load(g.getData().getPhoto()).placeholder(R.mipmap.picture).centerCrop().
                                        into(uploadPhote);
                            }



                        }
                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
        getpseronal();
    }

    private void getScore() {
        OkGo.<String>get(Comment_PerSonal_Show)

                .params("id", SPUtils.getAccounId(context))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        if (response.code() == 200) {
                            PersonalCommentShowBean personal = GsonUtil.parseJsonWithGson(response.body(), PersonalCommentShowBean.class);
                            if (Integer.valueOf(personal.getCode()) == 0) {

                                jiaoyi.setText(String.valueOf(personal.getData().getOrderCount()));
                                pingjia.setText(String.valueOf(personal.getData().getEvaluateCount()));
                                jiehuo.setText(personal.getData().getRate());
                                personalCommentAdapter.setNewData(personal.getData().getList());
                                smartrefresh.finishRefresh();

                            }

                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        showToast(response.message());
                    }
                });
    }

    private void selectInvite() {
        OkGo.<String>get(SelectInvite)

                .params("accountId", SPUtils.getAccounId(context))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        if (response.code() == 200) {
                            InviteCode personal = GsonUtil.parseJsonWithGson(response.body(), InviteCode.class);
                            if (Integer.valueOf(personal.getCode()) == 0) {

                                if (!personal.getData().getInviteCode().isEmpty()) {
                                    isinput = false;
                                    inviteCodeEdit.setTextColor(getResources().getColor(R.color.black));
                                    inviteCodeEdit.setText(personal.getData().getInviteCode());
                                    inviteCodeEdit.setEnabled(false);
                                }
                            } else {

                            }

                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        showToast(response.message());
                    }
                });
    }


    private void updateInvite(String inviteCode) {
        OkGo.<String>post(UpdateInvite)

                .params("accountId", SPUtils.getAccounId(context))
                .params("inviteCode", inviteCode)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        if (response.code() == 200) {
                            InviteCode personal = GsonUtil.parseJsonWithGson(response.body(), InviteCode.class);
                            if (Integer.valueOf(personal.getCode()) == 0) {
                                showToast(personal.getMsg());

                            } else {
                                showToast(personal.getMsg());
                                inviteCodeEdit.setText("");
                            }

                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        showToast(response.message());
                    }
                });
    }
}
